package thread;

public class DeadLock {

    private Object lockA = new Object();
    private Object lockB = new Object();

    public static void main(String[] args) throws InterruptedException {

        DeadLock deadLock = new DeadLock();
        deadLock.dead();
        System.out.println(2);
    }

    public void dead() throws InterruptedException {
        Thread A = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lockA){
                    System.out.println(Thread.currentThread().getName() + "lockA");
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (lockB){
                        // 执行不到这里
                        System.out.println("2秒钟后，"+Thread.currentThread().getName()
                                + "lockB");
                    }
                }
            }
        });

        Thread B = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lockB){
                    System.out.println(Thread.currentThread().getName() + "lockB");
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (lockA){
                        // 执行不到这里
                        System.out.println("2秒钟后，"+Thread.currentThread().getName()
                                + "lockA");
                    }
                }
            }
        });

        A.start();
        B.start();
    }
}
